#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

#include <boost/date_time.hpp>
#include <boost/filesystem.hpp>
#include "mgr/refboxclient/RefBoxClient.h"

using namespace std;

void startGame(std::string testFile, const int GAME_TIME,	boost::asio::io_service & io_service,
		std::ofstream & resultFile){
	system(testFile.c_str());

	int goalBlue = 0;
	int goalYellow = 0;

	{
		RefBoxClient refBoxClient(io_service);

		boost::posix_time::ptime gameStart = boost::posix_time::second_clock::universal_time();
		boost::posix_time::ptime currTime;

		RefereeCmd prevCmd = CMD_UNDEFINED;

		while(true){
			currTime = boost::posix_time::second_clock::universal_time();

			if ((currTime - gameStart).total_seconds() > GAME_TIME) {
				break;
			}

			io_service.run_one();
			RefereeCmd cmd = refBoxClient.getLastCommand();

			if (cmd == prevCmd) continue;

			prevCmd = cmd;

			if (cmd == CMD_GOAL_SCORED_BLUE){
				++goalBlue;
				cout<<"* Goal for blue, res: "<<goalYellow<<" : "<<goalBlue<<endl;
			}

			if (cmd == CMD_GOAL_SCORED_YELLOW){
				++goalYellow;
				cout<<"* Goal for yellow, res: "<<goalYellow<<" : "<<goalBlue<<endl;
			}
		}
	}

	system("~/magisterka/workspace/mgr_tests/scripts/stop.sh");
	cout<<"* Game finished | YELLOW: "<<goalYellow<<" BLUE: "<<goalBlue<<endl;
	resultFile<<goalYellow<<" "<<goalBlue<<endl;
}

void moveStatistics(string resultDir, int gameNr)
{
	ostringstream oss;
	oss<<resultDir;
	oss<<"/";
	oss<<gameNr;
	oss<<"_stats";
	string statsDirName = oss.str();
	boost::filesystem::create_directory(statsDirName);
	string command = "mv ~/magisterka/workspace/autonomic_client/Debug/*.stats " + statsDirName;
	system(command.c_str());
}

void moveLogs(string resultDir, int gameNr)
{
	ostringstream oss;
	oss<<resultDir;
	oss<<"/";
	oss<<gameNr;
	oss<<"_logs";
	string logsDirName = oss.str();
	boost::filesystem::create_directory(logsDirName);
	string command = "mv ~/magisterka/workspace/autonomic_client/Debug/blue*dump.txt " + logsDirName;
	system(command.c_str());
	command = "mv ~/magisterka/workspace/autonomic_client/Debug/yellow*dump.txt " + logsDirName;
	system(command.c_str());
}

int main(int argc, char *argv[]){
	int GAME_TIME = 600;	//in seconds
	int GAME_NR = 50;

	if (argc < 2){
		cout<<"* Usage: mgr_tests [test_script_file] [game_time = 600 sec.] [game_nr = 50]"<<endl;
		exit(0);
	}

	std::string testFileName(argv[1]);

	if (3 <= argc){
		std::string gameTimeStr(argv[2]);
		istringstream iss(gameTimeStr);
		iss>>GAME_TIME;
	}

	if (4 <= argc){
		std::string gameNrStr(argv[3]);
		istringstream iss(gameNrStr);
		iss>>GAME_NR;
	}

	boost::filesystem::path testFileNamePath(testFileName);

	string resultDir = testFileNamePath.filename() + "_results";

	if (boost::filesystem::exists(resultDir) ){
		cout<<"* ERROR: Results dir for this script already exist!\n\t"<< resultDir <<endl;
		exit(0);
	}

	boost::filesystem::create_directory(resultDir);
	std::cout<<"* Created results dir: "<<resultDir<<std::endl;

	string resultsFileName = resultDir + "/results.txt";
	ofstream results(resultsFileName.c_str(), ios::out);

	cout<<"* Created results file: "<<resultsFileName<<endl;

	cout<<"* Game time: "<<GAME_TIME<<" [s] Nr of games: "<<GAME_NR<<endl;

	for (int i = 1; i <= GAME_NR; i++){
		boost::asio::io_service io_service;
		cout<<"* Game nr "<<i<<endl;
		startGame(testFileName, GAME_TIME, io_service, results);
		moveStatistics(resultDir,i);
		moveLogs(resultDir,i);
	}

	results.close();

	return 0;
}
